﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace The_Wumpers
{
	public partial class UIForm : Form
	{
		int originalExStyle = -1;
		bool enableFormLevelDoubleBuffering = true;
        GameObject MainObject = new GameObject();
		protected override CreateParams CreateParams
		{
           
			get
			{
				if (originalExStyle == -1)
					originalExStyle = base.CreateParams.ExStyle;

				CreateParams cp = base.CreateParams;
				if (enableFormLevelDoubleBuffering)
					cp.ExStyle |= 0x02000000;   // WS_EX_COMPOSITED
				else
					cp.ExStyle = originalExStyle;

				return cp;
			}
		} 
		private void TurnOffFormLevelDoubleBuffering()
		{
			enableFormLevelDoubleBuffering = false;
			this.MaximizeBox = true;
		}
		private void frmMain_Shown(object sender, EventArgs e)
		{
			TurnOffFormLevelDoubleBuffering();
		}
        int xVal;
        int yVal;
        int spriteNumLeft = 0;
        int spriteNumRight = 0;
        bool isSpriteLeft = true;
        Point initialPoint;
		public UIForm()
		{
			InitializeComponent();

            xVal = pictureBoxSprite.Location.X;
            yVal = pictureBoxSprite.Location.Y;
            initialPoint = pictureBoxSprite.Location;
            MainObject.Map1.moveTheWumpus();
		}
        public static void ThreadProc()
        {
            Application.Run(new StartScreen());
        }

        private void PressedKey(object sender, KeyEventArgs e)
        {
            Point currentLocation = pictureBoxSprite.Location;

            if (e.KeyCode == Keys.Up)
            {
                currentLocation.Y -= 5;

                if(isSpriteLeft)
                {
                    CycleLeft();
                }
                else
                {
                    CycleRight();
                }
            }
            else if (e.KeyCode == Keys.Down)
            {
                currentLocation.Y += 5;

                if (isSpriteLeft)
                {
                    CycleLeft();
                }
                else
                {
                    CycleRight();
                }
            }
            else if (e.KeyCode == Keys.Right)
            {
                currentLocation.X += 5;

                isSpriteLeft = false;

                CycleRight();
            }
            else if (e.KeyCode == Keys.Left)
            {
                currentLocation.X -= 5;

                isSpriteLeft = true;

                CycleLeft();
            }
            else if (e.KeyCode == Keys.S)
            {
                using (Shop popUpForm = new Shop())
                {
                    popUpForm.ShowDialog();
                    if (popUpForm.gainArrow == true) { popUpForm.gainArrow = false; MainObject.Map1.player1.Arrows++; }
                }
            }
            else if (e.KeyCode == Keys.M) { MainObject.Map1.moveTheWumpus(); }
            else if (e.KeyCode == Keys.C)
            {
                using (HelpScreen popUpForm = new HelpScreen())
                {
                    popUpForm.ShowDialog();
                }
            }

                bool wumphit = false;
                if (e.KeyCode == Keys.Insert) { if (MainObject.Map1.rooms[MainObject.Map1.player1.fireArrow()].northWest == MainObject.Map1.wumper.Location) { wumphit = true; Console.WriteLine("Wumpus Hit"); WumpusSighting.Text = "WumpusHit"; winner(); } else MainObject.Map1.moveTheWumpus(); }
                else if (e.KeyCode == Keys.Home) { if (MainObject.Map1.rooms[MainObject.Map1.player1.fireArrow()].north == MainObject.Map1.wumper.Location) { wumphit = true; Console.WriteLine("Wumpus Hit"); WumpusSighting.Text = "WumpusHit"; winner(); } else MainObject.Map1.moveTheWumpus(); }
                else if (e.KeyCode == Keys.PageUp) { if (MainObject.Map1.rooms[MainObject.Map1.player1.fireArrow()].northEast == MainObject.Map1.wumper.Location) { wumphit = true; Console.WriteLine("Wumpus Hit"); WumpusSighting.Text = "WumpusHit"; winner(); } else MainObject.Map1.moveTheWumpus(); }
                else if (e.KeyCode == Keys.Delete) { if (MainObject.Map1.rooms[MainObject.Map1.player1.fireArrow()].southWest == MainObject.Map1.wumper.Location) { wumphit = true; Console.WriteLine("Wumpus Hit"); WumpusSighting.Text = "WumpusHit"; winner(); } else MainObject.Map1.moveTheWumpus(); }
                else if (e.KeyCode == Keys.End) { if (MainObject.Map1.rooms[MainObject.Map1.player1.fireArrow()].south == MainObject.Map1.wumper.Location) { wumphit = true; Console.WriteLine("Wumpus Hit"); WumpusSighting.Text = "WumpusHit"; winner(); } else MainObject.Map1.moveTheWumpus(); }
                else if (e.KeyCode == Keys.PageDown) { if (MainObject.Map1.rooms[MainObject.Map1.player1.fireArrow()].southEast == MainObject.Map1.wumper.Location) { wumphit = true; Console.WriteLine("Wumpus Hit"); WumpusSighting.Text = "WumpusHit"; winner(); } else MainObject.Map1.moveTheWumpus(); }

                if (MainObject.Map1.player1.Location == MainObject.Map1.wumper.Location)
                {
                    using (Form1 popUpForm = new Form1())
                    {
                        popUpForm.ShowDialog();
                        if (popUpForm.correct == true) { MainObject.Map1.moveTheWumpus(); MainObject.Map1.moveTheWumpus(); MainObject.Map1.moveTheWumpus(); MessageBox.Show("You scared the wumpus"); }
                        else
                        {
                            MessageBox.Show("the wumpus has killed you");
                            this.Close();
                            System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProc));
                            t.Start();

                        }
                    }
                }

           int testroom = MainObject.Map1.movePlayer();
           if (testroom == 1)
           {
               eventtext.Text = "You were moved by a bat"; testroom = 0;
           }
           else if (testroom == 2)
           {
               eventtext.Text = "You fell in a pit"; testroom = 0;
           }
  
                pictureBoxSprite.Location = currentLocation;

            collisionID();
            ArrowCount.Text = ("Arrow(s): " + MainObject.Map1.player1.Arrows);
            GoldLabel.Text = ("Gold: " + MainObject.Map1.player1.Coins);
            if (MainObject.Map1.wumper.Location == MainObject.Map1.rooms[MainObject.Map1.player1.Location].northWest ||
                MainObject.Map1.wumper.Location == MainObject.Map1.rooms[MainObject.Map1.player1.Location].north ||
                MainObject.Map1.wumper.Location == MainObject.Map1.rooms[MainObject.Map1.player1.Location].northEast ||
                MainObject.Map1.wumper.Location == MainObject.Map1.rooms[MainObject.Map1.player1.Location].SouthWest ||
                MainObject.Map1.wumper.Location == MainObject.Map1.rooms[MainObject.Map1.player1.Location].South ||
                MainObject.Map1.wumper.Location == MainObject.Map1.rooms[MainObject.Map1.player1.Location].SouthEast)
            {
                WumpusSighting.Text = "WUMPUS HAS BEEN SIGHTED!";
            }
            else WumpusSighting.Text = "No sign of the wumpus yet";
        }

        private void CycleLeft()
        {
            switch (spriteNumLeft)
            {
                case 0:
                    pictureBoxSprite.Image = Properties.Resources.Sprite1;
                    spriteNumLeft++;
                    break;
                case 1:
                    pictureBoxSprite.Image = Properties.Resources.Sprite2;
                    spriteNumLeft++;
                    break;
                case 2:
                    pictureBoxSprite.Image = Properties.Resources.Sprite3;
                    spriteNumLeft++;
                    break;
                case 3:
                    pictureBoxSprite.Image = Properties.Resources.Sprite4;
                    spriteNumLeft++;
                    break;
                case 4:
                    pictureBoxSprite.Image = Properties.Resources.Sprite5;
                    spriteNumLeft++;
                    break;
                case 5:
                    pictureBoxSprite.Image = Properties.Resources.Sprite6;
                    spriteNumLeft++;
                    break;
                case 6:
                    pictureBoxSprite.Image = Properties.Resources.Sprite7;
                    spriteNumLeft++;
                    break;
                case 7:
                    pictureBoxSprite.Image = Properties.Resources.Sprite8;
                    spriteNumLeft++;
                    break;
                case 8:
                    pictureBoxSprite.Image = Properties.Resources.Sprite9;
                    spriteNumLeft = 0;
                    break;
            }
        }
        private void CycleRight()
        {
            switch (spriteNumRight)
            {
                case 0:
                    pictureBoxSprite.Image = Properties.Resources.Sprite1Right;
                    spriteNumRight++;
                    break;
                case 1:
                    pictureBoxSprite.Image = Properties.Resources.Sprite2Right;
                    spriteNumRight++;
                    break;
                case 2:
                    pictureBoxSprite.Image = Properties.Resources.Sprite3Right;
                    spriteNumRight++;
                    break;
                case 3:
                    pictureBoxSprite.Image = Properties.Resources.Sprite4Right;
                    spriteNumRight++;
                    break;
                case 4:
                    pictureBoxSprite.Image = Properties.Resources.Sprite5Right;
                    spriteNumRight++;
                    break;
                case 5:
                    pictureBoxSprite.Image = Properties.Resources.Sprite6Right;
                    spriteNumRight++;
                    break;
                case 6:
                    pictureBoxSprite.Image = Properties.Resources.Sprite7Right;
                    spriteNumRight++;
                    break;
                case 7:
                    pictureBoxSprite.Image = Properties.Resources.Sprite8Right;
                    spriteNumRight++;
                    break;
                case 8:
                    pictureBoxSprite.Image = Properties.Resources.Sprite9Right;
                    spriteNumRight = 0;
                    break;
            }
        }

        private bool CheckCollision(PictureBox box)
        {
		
			
			
			Point spriteLocation = pictureBoxSprite.Location;

			int xSprite = spriteLocation.X + pictureBoxSprite.Width / 2;
			int ySprite = spriteLocation.Y + pictureBoxSprite.Height / 2;

			if (xSprite > box.Left && xSprite < (box.Left + box.Width))
			{
				if (ySprite > box.Top && ySprite < (box.Top + box.Height))
				{
					pictureBoxSprite.Location = initialPoint;

					return true;
				}
			}

			return false;
        }
        private void winner()
        {
            MessageBox.Show("YOU KILLED IT!");
            this.Close();
            System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProc));
            t.Start();
        }

		private void collisionID()
		{
            if (CheckCollision(pictureBoxTop) == true)
            {
                bool tempcheck = MainObject.Map1.moveplayernorth();
                Console.WriteLine("Collision North");
                ActionBox.Text = ("Pick a Door");

                if (tempcheck == false)
                {
                    Console.WriteLine("That Door is locked");
                    ActionBox.Text = ("That Door is locked");
                }
                else MainObject.Map1.player1.Coins++;
            }

            if (CheckCollision(pictureBoxTopLeft) == true)
            {
                bool tempcheck = MainObject.Map1.moveplayernorthWest();
                Console.WriteLine("Collision Northwest");
                ActionBox.Text = ("Pick a Door");
                if (tempcheck == false)
                {
                    Console.WriteLine("That Door is locked");
                    ActionBox.Text = ("That Door is locked");
                }
                else MainObject.Map1.player1.Coins++;
            }

            if (CheckCollision(pictureBoxTopRight) == true)
            {
                bool tempcheck = MainObject.Map1.moveplayernorthEast();
                Console.WriteLine("Collision Northeast");
                ActionBox.Text = ("Pick a Door");
                if (tempcheck == false)
                {
                    Console.WriteLine("That Door is locked");
                    ActionBox.Text = ("That Door is locked");
                }
                else MainObject.Map1.player1.Coins++;
            }

            if (CheckCollision(pictureBoxBottomLeft) == true)
            {
                bool tempcheck = MainObject.Map1.moveplayersouthWest();
                Console.WriteLine("Collision Southwest");
                ActionBox.Text = ("Pick a Door");
                if (tempcheck == false)
                {
                    Console.WriteLine("That Door is locked");
                    ActionBox.Text = ("That Door is locked");
                }
                else MainObject.Map1.player1.Coins++;
            }

            if (CheckCollision(pictureBoxBottom) == true)
            {
                bool tempcheck = MainObject.Map1.moveplayersouth();
                Console.WriteLine("Collision South");
                ActionBox.Text = ("Pick a Door");
                if (tempcheck == false)
                {
                    Console.WriteLine("That Door is locked");
                    ActionBox.Text = ("That Door is locked");
                }
                else MainObject.Map1.player1.Coins++;
            }

            if (CheckCollision(pictureBoxBottomRight) == true)
            {
                bool tempcheck = MainObject.Map1.moveplayersouthEast();
                Console.WriteLine("Collision Southeast");
                ActionBox.Text = ("Pick a Door");
                if (tempcheck == false)
                {
                    Console.WriteLine("That Door is locked");
                    ActionBox.Text = ("That Door is locked");
                }
                else MainObject.Map1.player1.Coins++;
            }

            RoomNumber.Text = Convert.ToString(MainObject.Map1.player1.Location);

		}

        private void button1_Click(object sender, EventArgs e)
        {
            using (Shop popUpForm = new Shop())
            {
                popUpForm.ShowDialog();
            }
        }

        private void pictureBoxSprite_Click(object sender, EventArgs e)
        {

        }

        private void UIForm_Load(object sender, EventArgs e)
        {

        }

        private void RoomNumber_Click(object sender, EventArgs e)
        {

        }

        private void ActionBox_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
	}
}
